/*
 *  Copyright (C) <2009>  Pavel Vakeš <pavel.vakes@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <account.h>
#include <KDebug>

Account::Account(const KConfigGroup * config) : QObject()
{
	if (config) loadConfiguration(config);
	else {
		name = "";
		interval = 10;
		check_after_run = true;
		protocol = "POP3";
		port = 110;
		ssl = false;
		username = "";
		password = "";
		connection_timeout = 1;
		mail_client = "";
		certificates = QList<QByteArray>();
	}
}

Account::Account(const Account * account) : QObject() {
	name = account->name;
	interval = account->interval;
	check_after_run = account->check_after_run;
	protocol = account->protocol;
	host = account->host;
	port = account->port;
	ssl = account->ssl;
	username = account->username;
	password = account->password;
	connection_timeout = account->connection_timeout;
	mail_client = account->mail_client;
	certificates = account->certificates;
}

Account::~Account()
{
}

void Account::loadConfiguration(const KConfigGroup * config)
{
	name = config->readEntry("name", QString());
	interval = config->readEntry("interval", 0);
	check_after_run = config->readEntry("check_after_run", true);
	protocol = config->readEntry("protocol", QString());
	host = config->readEntry("host", QString());
	port = config->readEntry("port", 0);
	ssl = config->readEntry("ssl", false);
	username = config->readEntry("username", QString());
	password = config->readEntry("password", QString());
	connection_timeout = config->readEntry("connection_timeout", 0);
	mail_client = config->readEntry("mail_client", QString());

	QList<QByteArray> certificates_tmp = config->readEntry("certificates", QList<QByteArray>());

	for (int i = 0; i < certificates_tmp.size(); ++i) {
		QSslCertificate cert(certificates_tmp.at(i), QSsl::Pem);
		if (!cert.isNull() && cert.isValid()) certificates.append(certificates_tmp.at(i));
	}
}

void Account::saveConfiguration(KConfigGroup * config)
{
	config->writeEntry("name", name);
	config->writeEntry("interval", interval);
	config->writeEntry("check_after_run", check_after_run);
	config->writeEntry("protocol", protocol);
	config->writeEntry("host", host);
	config->writeEntry("port", port);
	config->writeEntry("ssl", ssl);
	config->writeEntry("username", username);
	config->writeEntry("password", password);
	config->writeEntry("connection_timeout", connection_timeout);
	config->writeEntry("mail_client", mail_client);
	config->writeEntry("certificates", certificates);
}

bool Account::isReadyForCheck() {
	return (
		!name.isEmpty()
		&& interval > 0
		&& (protocol == "POP3" || protocol == "IMAP4")
		&& !host.isEmpty()
		&& port > 0 && port < 65536
		&& !username.isEmpty()
		&& !password.isEmpty()
		&& connection_timeout > 0 && connection_timeout <= interval
	);
}
